package com.zzyl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author zhengyuan&junjun
 * @Date 2024-11-02
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    /**
     * 分页查询
     *
     * **/
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {

        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        //将DeviceDataPageReqDto 中的Long类型的startTime和endTime转换未LocalDateTime
        LocalDateTime startDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime endDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());
        //封装给deviceDataPageReqDto
        deviceDataPageReqDto.setStartDate(startDate);
        deviceDataPageReqDto.setEndDate(endDate);
        //执行数据库查询
       Page<DeviceDataVo> page = deviceDataMapper.findListByCondition(deviceDataPageReqDto);
        return PageResponse.of(page, DeviceDataVo.class);
    }
    /**
     * 用户端按日统计查询指标数据
     * **/

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //1.按日查询设备数据
        List<DeviceDataGraphVo>  deviceDataGraphVos =  deviceDataMapper.queryDeviceDataListByDay(
                iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));
       //将 deviceDataGraphVos 转换为map，时间为key,心率为value
        Map<String,Double> map = null;
        if (CollectionUtil.isNotEmpty(deviceDataGraphVos)){
            map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime,
                    DeviceDataGraphVo::getDataValue));
        }
        //2.构建24小时图形列表集合
        List<DeviceDataGraphVo> deviceDataGraph24Vos = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        for (DeviceDataGraphVo graph24Vo : deviceDataGraph24Vos) {
            Double value = map.get(graph24Vo.getDateTime());
            if (value != null){
                graph24Vo.setDataValue(value);
            }
        }

        return deviceDataGraph24Vos;
    }
  /**
   *
   * 用户端按周统计查询指标数据
   *
   * **/
    @Override
    public Object queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {

        //1.按日查询设备数据
        List<DeviceDataGraphVo>  deviceDataGraphVos =  deviceDataMapper. queryDeviceDataListByWeek(
                iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));
        //将 deviceDataGraphVos 转换为map，时间为key,心率为value
        Map<String,Double> map = null;
        if (CollectionUtil.isNotEmpty(deviceDataGraphVos)){
            map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime,
                    DeviceDataGraphVo::getDataValue));
        }
        //2.构建24小时图形列表集合
        List<DeviceDataGraphVo> deviceDataGraph7DayVos = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        for (DeviceDataGraphVo graph7DayVo : deviceDataGraph7DayVos) {
            Double value = map.get(graph7DayVo.getDateTime());
            if (value != null){
                graph7DayVo.setDataValue(value);
            }
        }

        return deviceDataGraph7DayVos;
    }



    }
